package com.backend.demo1.controller;

import com.backend.demo1.config.ApiResponse;
import com.backend.demo1.models.Article;
import com.backend.demo1.services.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // 创建新公告
    @PostMapping
    public ResponseEntity<ApiResponse<Article>> createArticle(@RequestBody Article article) {
        try {
            Article createdArticle = articleService.createArticle(article);
            return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success("公告创建成功",createdArticle));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.badRequest(e.getMessage()));
        }
    }

    // 获取公告列表
    @GetMapping
    public ResponseEntity<ApiResponse<List<Article>>> getArticles() {
        try {
            List<Article> articles = articleService.getArticle();
            return ResponseEntity.ok().body(ApiResponse.success(articles));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(ApiResponse.internalServerError(e.getMessage()));
        }
    }

    
    // 根据ID获取公告
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Article>> getArticleById(@PathVariable String id) {
        try {
            Article article = articleService.getArticleByID(id);
            return ResponseEntity.ok().body(ApiResponse.success(article));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse.notFound(e.getMessage()));
        }
    }

    // 删除公告
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<String>> deleteArticleById(@PathVariable String id) {
        try {
            articleService.deleteArticleByID(id);
            return ResponseEntity.ok().body(ApiResponse.success("公告删除成功",null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.internalServerError( e.getMessage()));
        }
    }

    // 更新公告内容
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<String>> updateArticle(@PathVariable String id, @RequestBody Article inputArticle) {
        try {
            articleService.updateArticle(id, inputArticle);
            return ResponseEntity.ok().body(ApiResponse.success("公告更新成功",null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.internalServerError( e.getMessage()));
        }
    }

    // 点赞公告
    @PostMapping("/{id}/like")
    public ResponseEntity<ApiResponse<String>> likeArticle(@PathVariable String id) {
        try {
            articleService.likeArticle(id);
            return ResponseEntity.ok().body(ApiResponse.success("点赞成功",null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.internalServerError( e.getMessage()));
        }
    }

    // 获取公告点赞数

}